<?php


namespace app\common\model;


use think\Db;
use think\Model;

class Message extends Model
{

    // 表名
    protected $name = 'message';
    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = '';
    // 追加属性
    protected $append = [
    ];

    static public function setMessage($user_id, $msg = '', $page = '', $is_read = 0)
    {
        Db::startTrans();
        try {

            self::create([
                'user_id' => $user_id,
                'msg' => $msg,
                'page' => $page,
                'is_read' => $is_read
            ]);

            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }
    }

    public function user()
    {
        return $this->belongsTo("User", 'user_id', 'id')->setEagerlyType(0);
    }
}